package com.inmaytide.website.controller.deploy;

import com.inmaytide.website.domain.vo.deploy.DeployInfoVo;
import com.inmaytide.website.facade.deploy.DeployInfoFacade;
import com.inmaytide.website.framework.bean.JsonResult;
import com.inmaytide.website.utils.log.annotation.LogInformation;
import com.inmaytide.website.utils.pagination.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("deploy/info")
public class DeployInfoController {

    @Autowired
    private DeployInfoFacade deployInfoFacade;

    @LogInformation(description = "部署历史管理页")
    @RequestMapping(value = "index")
    public String list(ModelMap model) {
        model.put("projectList", deployInfoFacade.findProjectsInUse());
        return "deploy/info/index";
    }

    @LogInformation(description = "部署历史分页")
    @RequestMapping(value = "page")
    public String page(HttpServletRequest request, Pagination<DeployInfoVo> pagination, ModelMap model) {
        model.put("pagination", deployInfoFacade.pagination(request, pagination));
        return "deploy/info/list";
    }

    @LogInformation(description = "加载部署信息")
    @RequestMapping(value = "load/{id}")
    public JsonResult load(@PathVariable Long id) {
        return deployInfoFacade.findOne(id);
    }

    @LogInformation(description = "新增部署")
    @RequestMapping(value = "add")
    public String add(ModelMap model) {
        model.put("projectList", deployInfoFacade.findProjectsInUse());
        return "deploy/info/add";
    }

    @LogInformation(description = "保存信息：执行部署")
    @RequestMapping(value = "save")
    public JsonResult save(DeployInfoVo info) {
        return deployInfoFacade.deploy(info);
    }

    @LogInformation(description = "预读压缩包")
    @RequestMapping(value = "sycReadZip")
    public JsonResult sycReadZip(@RequestParam MultipartFile file) {

        JsonResult result = new JsonResult(true, "");
        try {
            List<String> files = deployInfoFacade.readZip(file);
            result.setProperty("files", files);
            result.setMsg("读取成功！请确认部署文件完整：");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.setSuccess(false).setMsg("读取失败！" + e.getMessage());
        }
    }

}
